package com.mkh.st.admin.strategy;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mkh.st.admin.api.entity.SysUser;
import com.mkh.st.admin.api.entity.SysUserRoleScope;
import com.mkh.st.admin.api.vo.DataPermissionVO;
import com.mkh.st.admin.api.vo.ScopeDataDeatil;
import com.mkh.st.admin.api.vo.ScopeDataVO;
import com.mkh.st.admin.api.vo.UserRoleScopeVO;
import com.mkh.st.admin.mapper.SysUserMapper;
import com.mkh.st.common.core.constant.CommonConstants;
import com.mkh.st.common.core.constant.enums.UserStatusEnum;
import com.mkh.st.common.core.constant.enums.UserTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
public class UserStrategy extends BaseScopeStrategy {

	@Autowired
	private SysUserMapper sysUserMapper;

	private final List<Long> ids= Arrays.asList(-1L);


	@Override
	public void setDataPermission(DataPermissionVO vo, SysUserRoleScope sysUserRoleScope, Long tenantId) {
		if(ObjectUtil.isNotNull(sysUserRoleScope)) {
			if (CommonConstants.ONE.equals(sysUserRoleScope.getAllFlag())) {
				vo.setUserAll(Boolean.TRUE);
				List<SysUser> list = sysUserMapper.selectList(Wrappers.<SysUser>lambdaQuery()
						.eq(SysUser::getDelFlag, CommonConstants.NOT_DEL).eq(SysUser::getNormalFlag, UserStatusEnum.NORMAL.getStatus())
						.eq(SysUser::getNotShow,CommonConstants.ZERO)
						.eq(SysUser::getTenantId, tenantId).orderByDesc(SysUser::getUserId));
				if (CollectionUtil.isNotEmpty(list)) {
					List<Long> userIds = list.stream().map(SysUser::getUserId).collect(Collectors.toList());
					vo.setUserList(userIds);
				}
			} else {
				if(StrUtil.isNotBlank(sysUserRoleScope.getIds())){
					List<Long> allUserIds = Convert.toList(Long.class, StrUtil.split(sysUserRoleScope.getIds(), ','));
					//未删除未禁用查询
					List<SysUser> list = sysUserMapper.selectList(Wrappers.<SysUser>lambdaQuery()
							.eq(SysUser::getDelFlag, CommonConstants.NOT_DEL).eq(SysUser::getNormalFlag, UserTypeEnum.TENANT_USER.getCode())
							.eq(SysUser::getNotShow,CommonConstants.ZERO)
							.in(SysUser::getUserId, allUserIds).orderByDesc(SysUser::getUserId));
					if (CollectionUtil.isNotEmpty(list)) {
						List<Long> userIds = list.stream().map(SysUser::getUserId).collect(Collectors.toList());
						vo.setUserList(userIds);
					}else {
						vo.setUserList(ids);
					}
				}else {
					vo.setUserList(ids);
				}
			}
		}else {
			vo.setUserList(ids);
		}
	}

	@Override
	public Long setKey() {
		return 4L;
	}

	@Override
	public ScopeDataDeatil getScopeDataList(DataPermissionVO vo, SysUserRoleScope one,Long scopeId) {
		ScopeDataDeatil deatil=new ScopeDataDeatil();
		deatil.setAllFlag(vo.getUserAll());
		deatil.setScopeId(scopeId);
		List<ScopeDataVO> scopeList =new ArrayList<>();
		if(CollectionUtil.isEmpty(vo.getUserList())){
			return deatil;
		}
		//当前操作人的公司权限
		List<SysUser> list = sysUserMapper.selectBatchIds(vo.getUserList());
		//被操作账号已有的公司权限
		List<Long> exist=new ArrayList<>();
		boolean flag=false;
		if(ObjectUtil.isNotNull(one)){
			if(one.getAllFlag().equals(CommonConstants.ONE)){
				flag=true;
			}else if(StrUtil.isNotBlank(one.getIds())){
				exist=Convert.toList(Long.class, StrUtil.split(one.getIds(),','));
			}
		}
		if(CollectionUtil.isNotEmpty(list)){
			for (SysUser item:list) {
				ScopeDataVO scopeDataVO=new ScopeDataVO();
				scopeDataVO.setId(item.getUserId()).setCode(item.getPhone()).setName(item.getUsername());
				if(flag || exist.stream().filter(v ->v.equals(item.getUserId())).findFirst().isPresent()){
					scopeDataVO.setChecked(Boolean.TRUE);
				}
				scopeList.add(scopeDataVO);
			}
			deatil.setScopeList(scopeList);
		}
		return deatil;
	}

	@Override
	public void getUserDataPermission(DataPermissionVO vo, DataPermissionVO item) {
		if(CollectionUtil.isNotEmpty(item.getUserList())){
			if(vo.getUserAll()){
				return;
			}else if(item.getUserAll()){
				vo.setUserAll(item.getUserAll());
				vo.setUserList(item.getUserList());
			}else {
				if(CollectionUtil.isNotEmpty(vo.getUserList())){
					List<Long> collect = Stream.of(vo.getUserList(),item.getUserList()).flatMap(Collection::stream).distinct().collect(Collectors.toList());
					vo.setUserList(collect);
				}else {
					vo.setUserList(item.getUserList());
				}
			}
		}
	}

	@Override
	public List<Long> getList(Long tenantId) {
		List<SysUser> list = sysUserMapper.selectList(Wrappers.<SysUser>lambdaQuery()
				.eq(SysUser::getDelFlag, CommonConstants.NOT_DEL).eq(SysUser::getNormalFlag, UserStatusEnum.NORMAL.getStatus())
				.eq(SysUser::getNotShow,CommonConstants.ZERO)
				.eq(SysUser::getTenantId, tenantId).orderByDesc(SysUser::getUserId));
		if (CollectionUtil.isNotEmpty(list)) {
			List<Long> userIds = list.stream().map(SysUser::getUserId).collect(Collectors.toList());
			return userIds;
		}
		return null;
	}

	@Override
	public void getAllList(UserRoleScopeVO userRoleScopeVO, SysUserRoleScope scope, Long tenantId) {
		if(CommonConstants.ZERO.equals(scope.getAllFlag())&& StrUtil.isBlank(scope.getIds())){
			return;
		}
		Boolean all = false;
		if (CommonConstants.ONE.equals(scope.getAllFlag())) {
			all = true;
		}

		//未删除未禁用查询
		List<SysUser> list = sysUserMapper.selectList(Wrappers.<SysUser>lambdaQuery()
				.eq(SysUser::getDelFlag, CommonConstants.NOT_DEL)
				.eq(SysUser::getNormalFlag, UserTypeEnum.TENANT_USER.getCode())
				.eq(SysUser::getNotShow, CommonConstants.ZERO)
				.eq(SysUser::getTenantId, tenantId)
				.in(!all, SysUser::getUserId, Convert.toList(Long.class, StrUtil.split(scope.getIds(), ',')))
				.orderByDesc(SysUser::getUserId));
		if (CollectionUtil.isNotEmpty(list)) {
			List<ScopeDataVO> scopeList = new ArrayList<>();
			for (SysUser item : list) {
				ScopeDataVO scopeDataVO = new ScopeDataVO();
				scopeDataVO.setId(item.getUserId()).setCode(item.getPhone()).setName(item.getUsername());
				scopeList.add(scopeDataVO);
			}
			userRoleScopeVO.setScopeList(scopeList);
		}
	}

	@Override
	public Boolean checkPermission(SysUserRoleScope one, String name, Long tenantId) {
		return null;
	}
}
